package cn.stylefeng.guns.modular.students.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.students.entity.Students;
import cn.stylefeng.guns.modular.students.mapper.StudentsMapper;
import cn.stylefeng.guns.modular.students.model.params.StudentsParam;
import cn.stylefeng.guns.modular.students.model.result.StudentsResult;
import  cn.stylefeng.guns.modular.students.service.StudentsService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 学生 服务实现类
 * </p>
 *
 * @author CHENCHEN
 * @since 2022-07-19
 */
@Service
public class StudentsServiceImpl extends ServiceImpl<StudentsMapper, Students> implements StudentsService {

    @Override
    public void add(StudentsParam param){
        Students entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(StudentsParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(StudentsParam param){
        Students oldEntity = getOldEntity(param);
        Students newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public StudentsResult findBySpec(StudentsParam param){
        return null;
    }

    @Override
    public List<StudentsResult> findListBySpec(StudentsParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(StudentsParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(StudentsParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Students getOldEntity(StudentsParam param) {
        return this.getById(getKey(param));
    }

    private Students getEntity(StudentsParam param) {
        Students entity = new Students();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
